package com.yyge.controller.admin.categoryclass;

import com.yyge.common.controller.BaseController;
import com.yyge.common.enums.AckCode;
import com.yyge.common.util.WaimaiThreadLocal;
import com.yyge.common.vo.req.PageReq;
import com.yyge.common.vo.resp.PageResp;
import com.yyge.controller.admin.categoryclass.req.CategoryClassReq;
import com.yyge.controller.admin.login.resp.AdminLoginResp;
import com.yyge.controller.admin.mapstruct.CategoryClassConverter;
import com.yyge.entity.CategoryClass;
import com.yyge.entity.Merchandiser;
import com.yyge.service.CategoryClassService;
import com.yyge.common.vo.resp.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yyge.service.MerchandiserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.crypto.spec.PSource;
import java.util.*;

@Api(tags = "管理后台-商品分类列表")
@RestController
@RequestMapping("/api/merchandiseclass")
public class CategoryClassController extends BaseController {
    // 注入业务逻辑层
    private CategoryClassService service;

    @Autowired
    private MerchandiserService merchandiserService;

    @Autowired/*自动装配*/
    public void setService(CategoryClassService categoryclassService){
        this.service=categoryclassService;
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("data")/* GET(SELECT): 查询 */
    public R<PageResp<CategoryClass>> get(PageReq<CategoryClass> vo,CategoryClassReq req) throws Exception  {
         // 将对象进行转换过滤无效条件
        CategoryClass categoryclass = CategoryClassConverter.MAPPER.categoryclass2req(req);

        AdminLoginResp adminLoginResp = WaimaiThreadLocal.loginVOThreadLocal.get();

        //如果是商家
        if(adminLoginResp.getType()==2){
            req.setMerchantId(adminLoginResp.getId());
        }

        PageResp pageResp=service.search(req,vo);
        return R.okHasData(pageResp);
    }

    @ApiOperation(value = "根据ID查询")
    @GetMapping("{id}")/* GET(SELECT): 查询 */
    public R selectById(@PathVariable("id") Long id) throws Exception  {
         // 将对象进行转换过滤无效条件
        CategoryClass byId = service.getById(id);
        return R.ok(byId);
    }



    @PostMapping/* POST(CREATE): 新增商品 */
    public R post(@RequestBody CategoryClassReq req){
        CategoryClass categoryClass = CategoryClassConverter.MAPPER.categoryclass2req(req);
        QueryWrapper wrapper=new QueryWrapper<>(categoryClass);
        CategoryClass one = service.getOne(wrapper);
        Long id = WaimaiThreadLocal.loginVOThreadLocal.get().getId();
        categoryClass.setMerchantId(id);
//        System.out.println("有吗？？");
//        System.out.println(one);;

        if (Objects.isNull(one)) {
            Boolean i=service.save(categoryClass);
            return i?R.ok("新增成功！"):R.error(AckCode.SYSTEM_SERVICE_ADD_ERROR);
        }else {
            return R.error(AckCode.SYSTEM_SERVICE_ADD_ERROR_REPEAT);
        }
    }

    @PutMapping/* POST(CREATE): 更新 */
    public R out(@RequestBody CategoryClass categoryclass){
        Boolean i=service.updateById(categoryclass);
        return i?R.ok("修改成功！"):R.error(AckCode.SYSTEM_SERVICE_MODIFY_ERROR);
    }

    @PatchMapping/* PUT(UPDATE): 部分更新 */
    public R patch(@RequestBody CategoryClass categoryclass){
        return null;
    }

    @DeleteMapping("/{id}")/* DELETE(DELETE): 删除&&批量删除 */
    public R delete(@PathVariable("id") Integer[] list){
        boolean i = service.removeByIds(Arrays.asList(list));
        return i?R.ok("修改成功！"):R.error(AckCode.SYSTEM_SERVICE_DELETE_ERROR);
    }
}

